package com.biboheart.breport.engine.compile;

import com.biboheart.breport.engine.BrParser;
import com.biboheart.breport.template.BrTemplateNode;
import com.biboheart.breport.template.BrTemplateStruct;
import com.biboheart.brick.utils.CheckUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrParserDefault implements BrParser {
    @Override
    public List<Map<String, Object>> parse(List<Map<String, Object>> dataList, BrTemplateStruct templateStruct) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (CheckUtils.isEmpty(dataList)) {
            return list;
        }
        List<BrTemplateNode> colList = templateStruct.getCols();
        for (Map<String, Object> input : dataList) {
            Map<String, Object> map = new HashMap<>();
            for (BrTemplateNode templateNode : colList) {
                Extractor extractor = ExtractorFactory.getExtractor(templateNode);
                Object value = extractor.extract(input, templateNode);
                map.put(templateNode.getSn(), value);
            }
            list.add(map);
        }
        return list;
    }
}
